<?php
require 'functions.php';
require '../data/data.php';

//设定响应头 信息
header('Content-Type:text/html;Charset=utf-8');

if($_SERVER['REQUEST_METHOD'] !== 'POST') {
  header('HTTP/1.1 405 Method Not Allowed');
  return;
}

$email = input('email');
$password = input('password');
$pwd_again = input('pwd_again');
$username = input('username','youke');
$age = input('age',18);
$gender = input('gender',1);
$courseA = input('course',[]);
$courses = implode(', ',$courseA);
$birth = input('birth','');
$attr = input('attr',1);
$describ = input('describ','');


//验证邮箱
if (empty($email)) {
   error('邮箱不能为空','../user_info.php?op=1');
   return;
}
if(! filter_var($email, FILTER_VALIDATE_EMAIL)) {
   error('请输入正确的邮箱','../user_info.php?op=1');
   return;
}

// 验证密码
if (empty($password) || strlen($password) < 6 ) {
   error('密码长度不能小于6位字符','../user_info.php?op=1');
   return;
}
// 确认密码
if ($password != $pwd_again) {
    error('两次输入的密码必须一致','../user_info.php?op=1');
    return;
}

$password = password_hash($password,PASSWORD_BCRYPT);

$sql = "insert into users(email,user_name,password,gender,cuorses,birthday,attr,introducion) values(?,?,?,?,?,?,?,?)";
$args = [$email, $username, $password, $gender, $courses,$birth,$attr,$describ];
$affected = execute($sql, $args);

//根据影响行数判断sql语句是否执行成功
if ($affected) {
   redirect('新增用户信息成功');
} else {
   error('新增用户信息失败', '../user_info.php?op=1');
}


  ?>

  <!DOCTYPE html>
  <html>
  <head>
    <title></title>
  </head>
  <body>
    <ul>
      <li>邮箱：<?= $email?></li>
      <li>密码：<?= $password?></li>
      <li>用户名：<?= $username?></li>
      <li>年龄：<?= $age?></li>
      <li>性别：<?= $gender == 1 ?'男':'女'; ?></li>
      <li>主修：<?= implode(', ', $courseA)?></li>
      <li>出生日期：<?= $birth?></li>
    </ul>
  </body>
  </html>
  </html>